/*
 * @Author: vincent040 18620373040@163.com
 * @Date: 2023-08-13 17:15:03
 * @LastEditors: vincent040 18620373040@163.com
 * @LastEditTime: 2023-08-13 17:54:14
 * @FilePath: /gitee/i-teaching/04 - 数据结构与算法/第03章-基础数据结构 · 栈和队列/code/Y型车轨/stack.c
 * Copyright (c) 2023 by GEC, All Rights Reserved. 
 * @Description: 
 */
 #include "stack.h"

linkstack * init_stack()
{
    linkstack *s = calloc(1, sizeof(linkstack));
    if(s != NULL)
    {
        s->top    = NULL;
        s->number = 0;
    }
    return s;
}

node *new_stack_node(int data)
{
    node *new = calloc(1, sizeof(node));
    if(new != NULL)
    {
        new->data = data;
        new->next = NULL;
    }
    return new;
}

void push(linkstack *s, int data)
{
    node *new = new_stack_node(data);
    if(new == NULL)
        return;

    new->next = s->top;
    s->top = new;
    s->number++;
}

bool stack_is_empty(linkstack *s)
{
    return s->number == 0;
}

node *pop(linkstack *s)
{
    if(stack_is_empty(s))
        return NULL;

    node *tmp = s->top;
    s->top = s->top->next;
    s->number--;

    tmp->next = NULL;
    return tmp;
}

node *top(linkstack *s)
{
    return stack_is_empty(s) ? NULL : s->top;
}